package com.eas.task.examStatistic;

import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.eas.biz.dao.exam.dataobject.ExamHistoryDO;
import com.eas.biz.dao.exam.dataobject.ExamHistoryQuery;
import com.eas.biz.dao.exam.interfaces.ExamHistoryDao;
import com.eas.biz.service.exam.domain.Exam;
import com.eas.biz.service.exam.domain.ExamHistoryConstants;
import com.eas.biz.service.exam.interfaces.ExamService;
import com.eas.task.common.interfaces.EasTaskJob;

public class BaseStatisticJob implements EasTaskJob, ApplicationContextAware {

    private String statisticTarget;

    private String statisticExamIds;

    private ApplicationContext context;

    @Resource(name = "examService")
    private ExamService examService;

    @Autowired
    private ExamHistoryDao examHistoryDao;

    public void work() {
        List<Long> examList = getNeedStatisticExamList();
        if (examList != null) {
            for (Long examId : examList) {
                System.out.println("Start statistic for exam=" + examId);

                statisticExam(examId);
            }
        }
    }

    private void statisticExam(Long examId) {
        Exam examDetail = examService.getExamDetail(examId);
        if (examDetail == null) {
            return;
        }

        if (statisticTarget.contains(SchoolStatisticJob.JOB_TARGET)
                && statisticTarget.contains(ClassStatisticJob.JOB_TARGET)) {
            ExamHistoryQuery query = new ExamHistoryQuery();
            query.setExamId(examId);
            query.setActorType(ExamHistoryConstants.ACTOR_TYPE_SCHOOL);
            List<ExamHistoryDO> schoolList = examHistoryDao.selectByQuery(query);
            if (schoolList != null) {
                for (ExamHistoryDO examHistoryDO : schoolList) {
                    SchoolStatisticJob job = (SchoolStatisticJob) context
                            .getBean("schoolStatisticJob");
                    job.setExamId(examHistoryDO.getExamId());
                    job.setSchoolId(examHistoryDO.getActorId());
                    job.work();
                }
            }
        }

        if (statisticTarget.contains(StudentStatisticJob.JOB_TARGET)) {
            ExamHistoryQuery query = new ExamHistoryQuery();
            query.setExamId(examId);
            query.setActorType(ExamHistoryConstants.ACTOR_TYPE_STUDENT);
            query.setPageSize(100);
            query.setPageNumber(1);
            List<ExamHistoryDO> studentList = examHistoryDao.selectByQuery(query);
            while (studentList != null && studentList.size() > 0) {
                for (ExamHistoryDO examHistoryDO : studentList) {
                    StudentStatisticJob job = (StudentStatisticJob) context
                            .getBean("studentStatisticJob");
                    job.setExamId(examId);
                    job.setExamDetail(examDetail);
                    job.setStudentId(examHistoryDO.getActorId());
                    job.work();
                }
                query.setPageNumber(query.getPageNumber() + 1);
                studentList = examHistoryDao.selectByQuery(query);
            }
        }

        if (statisticTarget.contains(QuestionStatisticJob.JOB_TARGET)) {
            QuestionStatisticJob job = (QuestionStatisticJob) context
                    .getBean("questionStatisticJob");
            job.setExamId(examId);
            job.setExamDetail(examDetail);
            job.work();
        }
    }

    private List<Long> getNeedStatisticExamList() {
        List<Long> list = new LinkedList<Long>();
        if (StringUtils.isNotBlank(statisticExamIds)) {
            String[] ids = statisticExamIds.split(",");
            if (ids != null && ids.length > 0) {
                for (String idString : ids) {
                    if (StringUtils.isNumeric(idString)) {
                        list.add(Long.valueOf(idString));
                    }
                }
            }
        }
        return list;
    }

    public String getStatisticTarget() {
        return statisticTarget;
    }

    public void setStatisticTarget(String statisticTarget) {
        this.statisticTarget = statisticTarget;
    }

    public String getStatisticExamIds() {
        return statisticExamIds;
    }

    public void setStatisticExamIds(String statisticExamIds) {
        this.statisticExamIds = statisticExamIds;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

}
